
#include "rls-ptable.h"

namespace ns3{

	MapEntry::MapEntry(Vector position, Time time, bool researchFlag , uint8_t numSeq):
	m_position(position), m_speed(speed),
	{}



	void PositionTable::AddEntry(Ipv4Address id, Vector position, Time time, bool flag, uint8_t seq ){
		std::map<Ipv4Address, MapEntry >::iterator i = m_table.find(id);
		if(i != m_table.end () || id.IsEqual(i->first))
		{
			/*FIXME: lifetime??*/
			m_table.erase(id);
			m_table.insert(std::make_pair(id, MapEntry(position, Simulator::Now(), false, seq)));
			return;
		}

		m_table.insert(std::make_pair(id, MapEntry(position, Simulator::Now(), false, seq)));
	}

	Vector PositionTable::GetPosition(Ipv4Address id, Vector invVec){
		std::map<Ipv4Address, MapEntry >::iterator i = m_table.find(id);
		if(i == m_table.end () && !id.IsEqual(i->first))
		{
			return invVec;
		}
		return i->second.GetPosition();
	}

	//Assuming that the location service will only set a new location if is more updated
	void PositionTable::SetPosition(Ipv4Address id, Vector position){
		std::map<Ipv4Address, MapEntry >::iterator i = m_table.find(id);
		if(i == m_table.end () && !id.IsEqual(i->first))
		{
			AddEntry(id, position);
		}
		return i->second.SetPosition(position);
	}

	void PositionTable::SetSpeed(Ipv4Address id,int speed){
		std::map<Ipv4Address, MapEntry >::iterator i = m_table.find(id);
		if(i == m_table.end () && !id.IsEqual(i->first))
		{
			return;
		}
		i->second.SetSpeed(speed);
	}

	void PositionTable::DeleteEntry(Ipv4Address id){
		m_table.erase(id);
	}

	void PositionTable::Purge(){
		std::map<Ipv4Address, MapEntry >::iterator i;
	/*	for(i = m_table.begin(); !(i == m_table.end()); i++)
		{
			if(m_entryLifeTime + GetEntryUpdateTime(i->first) <= Simulator::Now())
			{
				m_table.erase(i->first);
			}	
		}
		/* last element is not used in for cicle*/
	/*	if(m_entryLifeTime + GetEntryUpdateTime(i->first) <= Simulator::Now())
		{
			m_table.erase(i->first);
		}*/
	}

	void PositionTable::Clear(){
		m_table.clear ();
	}

}
